package com.flow.manager.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.flow.model.BankMessage;
import com.flow.model.PagerModel;
import com.flow.service.IBankMessageService;
import com.flow.tools.StringTools;

public class BankMessageAction {

	private IBankMessageService<BankMessage> bmService;

	public String insertBankMessage() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String bankName = request.getParameter("bmBankName");
		String number = request.getParameter("bmNumber");
		String cardHolder = request.getParameter("bmCardHolder");
		String status = request.getParameter("bmStatus");
		String remark = request.getParameter("bmRemark");
		status = !StringTools.nullOrEmpty(status) ? "1" : status;

		try {

			BankMessage bm = new BankMessage(bankName, number, cardHolder,
					Integer.valueOf(status), remark);
			if (bmService.insertBankMessage(bm))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";

	}

	public String findAllBankMessgeByPager() {

		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			PagerModel pm = this.bmService.findAllBankMessageByPager();
			request.setAttribute("pm", pm);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	public String inputUpdate() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("bmId");

		try {

			BankMessage bm = bmService.findBankMessageById(id);
			request.setAttribute("bm", bm);
			return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	public String updateBankMessage() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("bmId");
		String bankName = request.getParameter("bmBankName");
		String number = request.getParameter("bmNumber");
		String cardHolder = request.getParameter("bmCardHolder");
		String status = request.getParameter("bmStatus");
		String remark = request.getParameter("bmRemark");
		status = !StringTools.nullOrEmpty(status) ? "1" : status;
		try {

			if(!StringTools.nullOrEmpty(id))
				return "ERROR";
			BankMessage bm = new BankMessage(Integer.valueOf(id), bankName, number, cardHolder,
					Integer.valueOf(status), remark);
			if (bmService.updateBankMessage(bm))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}
	
	public String deleteBankMessage(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("bmId");
		
		try {
			
			if(!StringTools.nullOrEmpty(id))
				return "ERROR";
			BankMessage bm = new BankMessage();
			bm.setBmId(Integer.valueOf(id));
			
			if(bmService.deleteBankMessage(bm))
				return "SUCCESS";
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	public String findAllBankMessage(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			
			List<BankMessage> bmList = bmService.findAllBankMessage();
			request.setAttribute("bmList", bmList);
			return null;
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return "ERROR";
	}
	
	public void setBmService(IBankMessageService<BankMessage> bmService) {
		this.bmService = bmService;
	}

}
